package com.ccl.iot.device;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.ccl.iot.App;
import com.ccl.iot.batch.IOTBatch;
import com.ccl.iot.dvr.utils.Consts;
import com.ccl.iot.privates.BitmapCollector;
import com.ccl.iot.publics.UIUtils;

public class BatchIconProvider {

    public synchronized static Bitmap Build(int width, int height, int backColor, int imgIndex) {
        if (width > 0 && height > 0) {
            Bitmap bitmap;
            String key = "BatchIcon:" + width + "x" + height + ":" + backColor + "." + imgIndex;

            if ((bitmap = BitmapCollector.Get(key)) == null) {
                bitmap = Bitmap.createBitmap(width, height, backColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_4444);

                Canvas canvas = new Canvas(bitmap);

                Bitmap centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), Consts.INTENTION_BATCH_IMG_IDS.get(imgIndex));

                Paint paint = new Paint();

                int size = width > height ? height : width;

                centerIcon = UIUtils.scaleBitmap(centerIcon, size, size);

                float left = (width - centerIcon.getWidth()) / 2;
                float top = (height - centerIcon.getHeight()) / 2;

                if (backColor != Color.TRANSPARENT) {
                    canvas.drawColor(backColor);
                }

                canvas.drawBitmap(centerIcon, left, top, paint);

                canvas.save();
                canvas.restore();

                BitmapCollector.Add(key, bitmap);

                return bitmap;
            } else {
                return bitmap;
            }
        }

        return null;
    }

    public static Bitmap Build(IOTBatch batch,int bgColor, int width, int height) {
        if (batch != null) {
            return Build(width, height, bgColor, batch.getImgIndex());
        }
        return null;
    }

}
